/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bll;

import dal.QuanAoDAL;
import bean.Argument;
import bean.NguoiDung;
import bean.ThongTinNguoiDung;
import supporter.Supporter;

/**
 *
 * @author HoangNam
 */
public class NguoiDungBLL {

    private static NguoiDungBLL control;
    private QuanAoDAL dal;

    public static NguoiDungBLL init() {
        if (control == null) {
            control = new NguoiDungBLL();
        }
        return control;
    }

    public NguoiDungBLL() {
        dal = new QuanAoDAL();
    }

    public NguoiDung login(String username, String pass) {
        NguoiDung user = getUserByUsername(username);
        if (user != null) {
            String str = Supporter.md5Encrypt(pass);
            if (user.getPassword().equals(Supporter.md5Encrypt(pass))) {
                ThongTinNguoiDung info = getUserInfoByID(user.getUserId());
                user.setThongTinNguoiDung(info);
                return user;
            }
        }
        return null;
    }

    public ThongTinNguoiDung getUserInfoByID(int id) {
        String sql = "from ThongTinNguoiDung where userid = :userid";
        Argument[] arr = new Argument[]{new Argument("userid", id)};
        ThongTinNguoiDung info = (ThongTinNguoiDung) dal.getSingleResult(sql, arr);
        return info;
    }

    private NguoiDung getUserByUsername(String username) {
        String cmd = "from NguoiDung where username = :username";
        Argument[] arr = new Argument[]{new Argument("username", username)};
        return (NguoiDung) dal.getSingleResult(cmd, arr);
    }
}
